//
//  XJShouCangGoodsViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/14.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  我的收藏>收藏的商品

#import "XJShouCangGoodsViewController.h"
#import "RBGoodsDetailViewController.h" //7-8商品详情

#import "XJBottomView.h"

#import "XJShouCangGoodsItem.h" //cell
#import "XJShouCangGoodsModel.h" //model
#import "XJSCGoodsClvProxy.h" //collectionView代理方法
#import "XJSCGoodsFormat.h" // 处理各种业务逻辑


@interface XJShouCangGoodsViewController ()<XJSCGoodsFormatDelegate>
@property (nonatomic, strong) UICollectionView *clv;
@property (nonatomic, strong) XJSCGoodsClvProxy *goodsClvProxy;
@property (nonatomic, strong) XJSCGoodsFormat *goodsFormat;

/**底部view*/
@property (nonatomic, strong) XJBottomView *bottomView;

@end

@implementation XJShouCangGoodsViewController

#pragma mark - 一 lazy
- (UICollectionView *)clv {
    if (!_clv) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
        _clv = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        
        [_clv registerClass:[XJShouCangGoodsItem class] forCellWithReuseIdentifier:@"XJShouCangGoodsItem"];
        
        _clv.backgroundColor = [UIColor whiteColor];
        
        _clv.delegate = self.goodsClvProxy;
        _clv.dataSource = self.goodsClvProxy;
    }
    return _clv;
}
- (XJSCGoodsClvProxy *)goodsClvProxy {
    if (!_goodsClvProxy) {
        _goodsClvProxy = [[XJSCGoodsClvProxy alloc] init];
        
        XJWeakSelf(self);
        
        _goodsClvProxy.xjscgoodsclvProxySelecteCellBlock = ^(NSIndexPath *indexPath, BOOL isEditing, BOOL isModelSelected, XJShouCangGoodsModel *selModel) {
            if (isEditing) {
                [weakself.goodsFormat xjscgoods_selectItemAtIndexPath:indexPath isSelected:isModelSelected];
            }else {
                
                
                // 跳转商品详情
                XJLog(@"%@",selModel.goodsId);
                
                RBGoodsDetailViewController *goodVC = [[RBGoodsDetailViewController alloc] init];
                goodVC.goodsId = selModel.goodsId;
                
                [weakself.navigationController pushViewController:goodVC animated:YES];
            }
        };
    
    }
    return _goodsClvProxy;
}

- (XJSCGoodsFormat *)goodsFormat {
    if (!_goodsFormat) {
        _goodsFormat = [[XJSCGoodsFormat alloc] init];
        _goodsFormat.delegate = self;
    }
    return _goodsFormat;
}

- (XJBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[XJBottomView alloc] initWithFrame:CGRectMake(0, KHeight, KWidth, 50)];
        [self.view addSubview:_bottomView];
        
        XJWeakSelf(self);
        _bottomView.xjAllSeletedBlock = ^(BOOL btnSelected) {
            [weakself.goodsFormat xjscgoods_selecteaAllWithState:btnSelected];
        };
        
        _bottomView.xjDeleBlock = ^{
            [weakself.goodsFormat beginToDeleteSelectedGoods];
        };
    }
    return _bottomView;
}


#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    [self setupView];
}

//添加view
- (void)setupView {
    CGRect tempFrame = self.view.bounds;
    tempFrame.size.height = (KHeight - 40 - kStatusBarAndNavigationBarHeight);
    self.clv.frame = tempFrame;
    
    [self.view addSubview:self.clv];
    
    [self requestData];
}

//layout view

#pragma mark - 三 网络请求
- (void)requestData {
    [self.goodsFormat requestSCGoodsList];
}

#pragma mark - 四 XJSCGoodsFormatDelegate
- (void)requestSCGoodsListSuccessWithArray:(NSMutableArray *)dataArray {
    
    self.goodsClvProxy.dataArray = dataArray;
}

- (void)xjscgoodsFormatWillDeleteSelectedTopics:(NSArray *)selectedArr {
    
    XJLog(@"=====:%ld",selectedArr.count);
    
    if(selectedArr.count == 0) {
        [JRToast showWithText:@"强选择要删除的商品"];
        return;
    }
    
    [self.goodsFormat deleteSelectedGoodsArr:selectedArr];
}

- (void)xjscgoodsFormatReloadDataWhenNeed {
    [self.clv reloadData];
}



#pragma mark - 五 底部view业务逻辑
- (void)setScgoodsvcIsEditing:(BOOL)scgoodsvcIsEditing {
    _scgoodsvcIsEditing = scgoodsvcIsEditing;
    
//    XJLog(@"=====:%@",scgoodsvcIsEditing ? @"YES":@"NO");

    [self showAndHideBottomViewAnimation:scgoodsvcIsEditing];
    
    self.goodsClvProxy.isScgoodsClvEditing = scgoodsvcIsEditing;
    [self.clv reloadData];
}


- (void)showAndHideBottomViewAnimation:(BOOL)isEditing {
    if (isEditing) {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.frame = CGRectMake(0, KHeight - 40 - kStatusBarAndNavigationBarHeight - 50, KWidth, 50);
        }];
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.frame = CGRectMake(0, KHeight, KWidth, 50);
        }];
    }
}





@end
